package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Family;
import cn.wolfcode.crm.query.QueryForObject;
import cn.wolfcode.crm.service.IFamilyService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("family")
public class FamilyController {
    @Autowired
    private IFamilyService familyService;

    @RequiresPermissions("family:index")
    @PermissionName("家庭首页")
    @RequestMapping("index")
    public String index() {
        return "family/list";
    }

    @RequiresPermissions("grade:list")
    @PermissionName("班级管理列表")
    @ResponseBody
    @RequestMapping("list")
    public PageResult list(Model model, @ModelAttribute("qo")QueryForObject qo){
        return familyService.query(qo);
    }





    @RequiresPermissions("family:save")
    @PermissionName("家庭保存")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Family family){
        JsonResult result = new JsonResult();
        try {
            familyService.insert(family);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("保存家庭失败");
        }
        return result;
    }

    @RequiresPermissions("family:update")
    @PermissionName("家庭编辑")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Family family){
        JsonResult result = new JsonResult();
        try {
            familyService.updateByPrimaryKey(family);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("更新家庭失败");
        }
        return result;
    }
    @RequiresPermissions("family:delete")
    @PermissionName("家庭编辑")
    @RequestMapping("delete")
    public void delete(Long empId){

            familyService.delete(empId);



    }


    @RequestMapping("selectAll")
    @ResponseBody
    public List<Family> selectAll(){
        return familyService.selectAll();
    }
    @RequestMapping("queryByEmpId")
    @ResponseBody
    public List<Family> queryByEmpId(Long empId){
        return familyService.queryByEmpId(empId);
    }

}
